﻿
@{
    ViewBag.Title = "用户管理";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>人员信息管理</legend>
</fieldset>
<form class="layui-form" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label">搜索</label>
        <div class="layui-input-block">
            <input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="输入名称" class="layui-input" style="width:300px;float:left;">
            <input type="text" value="" style="display:none" />
            <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
            <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnAddUser"><i class="layui-icon">&#xe61f;</i>新增</button>

            @*<button type="button" class="layui-btn layui-btn-normal" onclick="apply()"><i class="layui-icon">&#xe63b;</i></button>*@
        </div>
    </div>
</form>
<table id="demo" lay-filter="UserTableList"></table>
<div id="addLayer" style="display:none;">
    <form class="layui-form" id="addCardFrom" style="margin-top:10px;">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="UserName" lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">电话</label>
                <div class="layui-input-inline">
                    <input type="tel" name="Tel" lay-verify="required|phone" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门</label>
            <div class="layui-inline">
                <input type="text" name="UserName" lay-verify="required" class="layui-input">
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <label class="layui-form-label"></label>
                    <button type="button" class="layui-btn" id="BtnSave">保存</button>
                </div>
            </div>
        </div>
        <button type="submit" id="mysubmit" class="layui-btn" lay-submit lay-filter="formVerify" style="display:none;">立即提交</button>
    </form>
</div>
<script type="text/javascript">
    var AuthList = JSON.parse('@Html.Raw(Json.Encode(ViewBag.AuthList))');
    function SearchAuth(Type,Name) {
        var IsGet = false;
        for (var i = 0; i < AuthList.length; i++) {
            if (AuthList[i].Type == Type && AuthList[i].Name == Name) {
                IsGet = true;             
            }
        }
        return IsGet;
    }
    var table;
    layui.use(['form','table', 'upload', 'layer'], function () {
        var upload = layui.upload;
        table = layui.table;
        var tab = parent.tab;
        var layer = layui.layer;
        var index;

        $("#BtnAddUser").on("click", function () {
            //$("#addCardFrom").css("display", "block");
            if (false == SearchAuth("UserAuth", "用户管理")) {
                alert("没有权限");
                return;
            }
            index = layer.open({
                type: 2,
                area: ['700px', '200px'],
                content: '/User/AdminUserEditView?option=add' //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            });
        });

        $("#BtnSave").on("click", function () {
            layer.close(index);
        });

        //执行实例
        var uploadInst = upload.render({
            elem: '#UpLoadExcel' //绑定元素
            , url: '/GoodsInfo/Upload/' //上传接口
            , accept: 'file'
            , size: 10240
            , exts: "xls|xlsx"
            , done: function (res) {
                //上传完毕回调
                alert(res.error);
                table.reload("demo", {
                    //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                });
            }
            , error: function () {
                //请求异常回调
                alert("请求异常");
            }
        });

        ////监听单元格编辑
        //table.on('edit(UserTableList)', function (obj) {
        //    var value = obj.value //得到修改后的值
        //        , data = obj.data //得到所在行所有键值
        //        , field = obj.field; //得到字段
        //    layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);
        //});

        //第一个实例
        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/User/UserTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "UserId", Order: "Desc", Condition: " and IsDelete<>1" }
            , limits: [10, 50, 100]

            , cols: [[ //表头
                { field: 'UserName', title: '姓名', width: 150, fixed: 'left' }
                , {
                    field: "TempUserRight", title: "权限", width: 100,
                    templet: function (d) {
                        return '预留';
                    }
                }
                , { field: 'Department', title: '部门', width: 100 }
                , { field: 'Tel', title: '电话', width: 120 }
                , {
                    field: "operation", title: "操作", width: 250,
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="RestPW">重置密码</a>' +
                            '<a class="layui-btn layui-btn-xs" lay-event="detail">编辑</a><a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>' +
                            '<a class="layui-btn layui-btn-xs" lay-event="Authority">权限</a>';
                    }
                }
            ]]
        });

        table.on("tool(UserTableList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'detail') {
                //alert(data.ArticleId);
                if (false == SearchAuth("UserAuth", "用户管理")) {
                    alert("没有权限");
                    return;
                }
                index = layer.open({
                    type: 2,
                    area: ['700px', '200px'],
                    content: '/User/AdminUserEditView?option=edit&UserID=' + data.UserId //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
                //tab.tabAdd(options);
                //parent.layui.index.openTabsPage("/User/UserEdit","JS新增")

            }

            if (layEvent == 'Authority') {
                if (false == SearchAuth("UserAuth", "用户管理")) {
                    alert("没有权限");
                    return;
                }
                parent.layui.index.openTabsPage('/Authority/AuthorityView?UserId=' + data.UserId, data.UserName+'权限');

            }


            if (layEvent == 'TableDel') {
                if (false == SearchAuth("UserAuth", "用户管理")) {
                    alert("没有权限");
                    return;
                }
                layer.msg('暂时无法删除', { icon: 0, time: 1000 });
            }

            if (layEvent == 'RestPW') {
                if (false == SearchAuth("UserAuth", "用户管理")) {
                    alert("没有权限");
                    return;
                }
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要重置该用户登录密码？', {
                    btn: ['确定重置', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        //SSEditDelete
                        $.ajax({
                            type: "POST",
                            url: "/User/RestPW",
                            async: false,
                            data: { "UserId": data.UserId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('密码重置为"123456"', { icon: 6, time: 1000 });
                                    table.reload("demo", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("重置失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

        });

    });

    //回车键
    function getKey() {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }

    function SearchFunc() {
        var text = $("#SearchText").val();
        if (text == "") {
            table.reload("demo", {
                where: {
                    SortName: "UserId", Order: "Desc", Condition: " and IsDelete<>1"
                },
                page: {
                    curr: 1
                }
            });
        }
        else {
            table.reload("demo", {
                where: {
                    SortName: "UserId", Order: "Desc", Condition: " and IsDelete<>1 and UserName like '%" + text + "%'"
                },
                page: {
                    curr: 1
                }
            });
        }
    }

</script>